# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import csv
import os

from itemadapter import ItemAdapter


class PpNewsPipeline:
    def process_item(self, item, spider):
        # 判断是否存在download文件夹，如果没有则创建
        download_path = os.getcwd() + '/download/'  # 当前文件夹下的download文件夹
        if not os.path.exists(download_path):  # 判断文件夹或文件
            os.makedirs(download_path)
        msg_type = item.get('type')
        if msg_type == 'info':
            with open(download_path + './澎湃新闻.csv', 'a', encoding='utf-8')as g:
                # 创建一个csv的DictWriter对象，这样才能够将写入csv格式数据到这个文件
                g_csv = csv.DictWriter(g, ['new_title', 'new_img', 'new_url', 'new_time'])
                # 写入多行行（当做数据）
                # info = ['movie_title', 'img_src', 'rating_num', 'people_num']
                # g_csv.writeheader()
                item.pop("type")  # 删除type 这个key-value
                g_csv.writerows([item])
                print("保存信息到CSV....ok")
                return item
        elif msg_type == 'img':
            # 存储图片
            img_name = item.get("img_name")
            img_bytes = item.get("img_bytes")
            with open(download_path + img_name, "wb") as f:
                f.write(img_bytes)
                print("保存图片%s完成....ok" % img_name)
